package audio;

import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Vector;

import chords.TimedChord;

public class SABFileManager {

    public static Vector<TimedChord> loadFile(File file) throws FileNotFoundException, IOException, ClassNotFoundException {
	if (!file.exists())
	    throw new FileNotFoundException(file.getAbsolutePath());

	ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));

	Vector<TimedChord> returnData = new Vector<TimedChord>();

	try {
	    while (true) {
		returnData.add((TimedChord) ois.readObject());
	    }
	} catch (EOFException e) {
	    return returnData;
	}
    }

    public static void saveFile(File f, Vector<TimedChord> data) throws IOException {
	if (!f.getName().endsWith(".abf"))
	    f = new File(f.getAbsolutePath() + ".abf");
	ObjectOutputStream oos;
	f.createNewFile();
	oos = new ObjectOutputStream(new FileOutputStream(f));

	for (TimedChord tc : data) {
	    oos.writeObject(tc);
	}
	oos.close();
    }
}